package com.canplay.screen.utils

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

object Md5Utils {
    private const val SALT = "test001"
    fun generateSaltedMD5Signature(timestamp: String, nonceStr: String): String? {
        // 构造待签名字符串
        val signString = """
              $timestamp
              $nonceStr
              $SALT
              """.trimIndent()
        return try {
            // 创建 MD5 摘要对象
            val md = MessageDigest.getInstance("MD5")
            // 更新摘要信息
            md.update(signString.toByteArray())
            // 计算摘要
            val digest = md.digest()
            // 将摘要转换为十六进制字符串
            val sb = StringBuilder()
            for (b in digest) {
                sb.append(String.format("%02x", b.toInt() and 0xff))
            }
            sb.toString()
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
            null
        }
    }
}